﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace ViewMaker.Core.Controls
{
    /// <summary>
    /// ラジオボタンリストViewコントロール
    /// </summary>
    public class RadioButtonListViewControl : ListBoxViewControl
    {
        /// <summary>
        /// プロパティ定数
        /// </summary>
        public new class Properties : ListBoxViewControl.Properties
        {
            public const string ItemWidth = "ItemWidth";
            public const string Orientation = "Orientation";
        }

#if !SILVERLIGHT
        const string templateResource = @"
<Style TargetType=""{{x:Type ListBox}}"">
    <Setter Property=""ItemsPanel"">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel {1} />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property=""ItemContainerStyle"">
        <Setter.Value>
            <Style TargetType=""{{x:Type ListBoxItem}}"">
                <Setter Property=""Template"">
                    <Setter.Value>
                        <ControlTemplate TargetType=""{{x:Type ListBoxItem}}"">
                            <RadioButton
                                Focusable=""False""
					            IsChecked=""{{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={{RelativeSource FindAncestor, AncestorType={{x:Type ListBoxItem}}}}}}""
                                {0}>
                                <ContentPresenter />
                            </RadioButton>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>";
#else
        const string controlTemplateResource = @"
    <Style TargetType=""ListBoxItem"" xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
        <Setter Property=""Template"">
            <Setter.Value>
                <ControlTemplate TargetType=""ListBoxItem"">
                    <Grid Background=""Transparent"">
                    <RadioButton IsHitTestVisible=""False"" IsTabStop=""False"" IsChecked=""{{TemplateBinding IsSelected}}"" {0}>
                        <ContentPresenter></ContentPresenter>
                    </RadioButton>
                        </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>";
        const string itemTemplateResource = @"
    <ItemsPanelTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
        <StackPanel {0}/>
    </ItemsPanelTemplate>";
#endif

        /// <summary>
        /// アイテム幅
        /// </summary>
        [Display(Name = "Item Width",GroupName=LayoutGroup)]
        public int? ItemWidth { get; set; }


        /// <summary>
        /// アイテムの配置方向
        /// </summary>
        [Display(GroupName = LayoutGroup)]
        [View(ViewControlType.ComboBox)]
        [ViewProperty(ComboBoxViewControl.Properties.ItemsSource, "OrientationList")]
        public LayoutOrientation Orientation { get; set; }

        #region ShouldSerialize

        public virtual bool ShouldSerializeItemWidth() { return ItemWidth.HasValue; }

        #endregion

        public RadioButtonListViewControl()
        {
            ControlType = ViewControlType.RadioButtonList;
            Orientation = ViewMakerSettings.Instance.DefaultRadioButtonOrientation;

        }
        public override List<XamlNode> CreateXaml(XamlNode parent)
        {
            var list = base.CreateXaml(parent);
            var box = list.FirstOrDefault(x => x.Name == "ListBox");
            if (box != null)
            {
                box.SetAttribute("IsTabStop", "False");
                if (!AdditionalProperty.ContainsKey("BorderThickness")) box.SetAttribute("BorderThickness", "0");
                string widthString = ItemWidth.HasValue ? "Width=\"" + ItemWidth.ToString() + "\"" : "";
                string orientaionString = "Orientation=\"" + Orientation.ToString() + "\"";
#if !SILVERLIGHT
                var resource = box.AddElement("ListBox.Resources");
                resource.InnerXml = string.Format(templateResource, widthString, orientaionString);
                resource.ChildElements[0].SetDefaultNamespace();
                var style = CreateStyle(box, "RadioButton");
                if (style != null) resource.AppendChild(style);

#else

                var panelStyle = box.AddElement("ListBox.ItemsPanel");

                string style=" ";
                var fontSize = GetFontSize();
                if (fontSize.HasValue) style += "FontSize=\"" + fontSize.ToString() + "\" ";
                var fore = GetForeground();
                if (!string.IsNullOrEmpty(fore)) style += "Foreground=\"" + fore + "\" "; 
                var back = GetBackground();
                if (!string.IsNullOrEmpty(back)) style += "Background=\"" + back + "\" ";

                panelStyle.InnerXml = string.Format(itemTemplateResource, orientaionString);
                var itemstyle = box.AddElement("ListBox.ItemContainerStyle");
                itemstyle.InnerXml = string.Format(controlTemplateResource, widthString + style);

#endif

            }
            return list;
        }
    }

}
